package com.LeeCode;

/**
 * 拼车
 */

public class Code1094 {
    public static void main(String[] args) {
        int[][] trips = {
                {2, 1, 5},
                {3, 3, 7}
        };
        int capacity = 5;
        System.out.println(new Code1094().carPooling(trips, capacity));
    }

    public boolean carPooling(int[][] trips, int capacity) {
        int[] diff = new int[1001];
        for (int[] trip : trips) {
            int num = trip[0], from = trip[1], to = trip[2];
            diff[from] += num;
            diff[to] -= num;
        }
        int sum = 0;
        for (int i = 0; i <= 1000; i++) {
            sum += diff[i];
            if (sum > capacity) return false;
        }
        return true;
    }
}
